import { combineReducers, createStore  } from 'redux'
import actionTypes from './actionTypes'

/**
 * 
 * @param {*} music {// 当前播放歌曲
 *  progress: 播放进度 0 ~ 100,
 *  length: 歌曲时长，单位秒,
 * }
 * @param {*} action 
 */
function musicReducer(music = {progress: 0, length: 260}, action){
  switch (action.type) {
    case actionTypes.SETPROGRESS:
      return {
        ...music,
        progress: action.payload.progress
      }
    default:
      return music;
  }
}

function playingReducer(playing = false, action){
  switch (action.type) {
    case actionTypes.SETPLAYING:
      return action.payload.playing;
    default:
      return playing;
  }
}
function newsReducer(news = [], action){
  switch (action.type) {
    case actionTypes.SETNEWS:
      return action.payload.news;
    default:
      return news;
  }
}
// 通过redux提供的combineReducers方法联合reducer
let reducer = combineReducers({
  music: musicReducer,
  playing: playingReducer,
  news: newsReducer
})
// applyMiddleware(thunk) 调用中间件thunk，这样才能在action中使用异步方法
// let store = createStore(reducer, applyMiddleware(thunk));
let store = createStore(reducer);
export default store;